<template>
    <view class="v-collapse">
        <view v-for="(item, index) in items" :key="index" :class="['collapse-item', { expanded: item.expanded }]">
            <view class="collapse-header" @click="toggleItem(index)">
                <view class="header-title">{{ item.title }}</view>
                <view v-if="!item.isRightIcon" class="header-icon">
                    <view class="arrow" :class="{ rotated: item.expanded }"></view>
                </view>
            </view>
            <view class="collapse-content">
                <view class="content-inner">
                    <slot name="content" :item="item">
                        {{ item.content }}
                    </slot>
                </view>
            </view>
        </view>
    </view>
</template>

<script lang="ts" setup>
import { ref, watch,inject } from 'vue';

interface CollapseItem {
    title: string;
    content: string;
    expanded: boolean;
    name?: string;
    isRightIcon?: boolean;
}

const props = defineProps({
    items: {
        type: Array as () => CollapseItem[],
        required: true,
        default: () => []
    },
    accordion: {
        type: Boolean,
        default: true
    },
    activeIndex: {
        type: Number,
        default: 0
    }
});

const emit = defineEmits(['update:activeIndex', 'change']);

const config = inject<any>('config');
const items = ref<CollapseItem[]>([...props.items]);

const initExpandedState = () => {
    items.value.forEach((item, index) => {
        item.expanded = index === props.activeIndex;
    });
};

watch(
    () => props.activeIndex,
    (newIndex) => {
        if (props.accordion) {
            items.value.forEach((item, index) => {
                item.expanded = index === newIndex;
            });
        }
    }
);

initExpandedState();

const toggleItem = (index: number) => {
    const item = items.value[index];

    if (props.accordion) {
        items.value.forEach((otherItem, otherIndex) => {
            otherItem.expanded = otherIndex === index ? !item.expanded : false;
        });
    } else {
        item.expanded = !item.expanded;
    }

    const activeIndex = items.value.findIndex((item) => item.expanded);
    emit('update:activeIndex', activeIndex);
    emit('change', activeIndex);
};
</script>

<style lang="scss" scoped>
.v-collapse {
    width: 100%;
    overflow: hidden;
    box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.08);

    .collapse-item {
        margin-bottom: 12rpx;
        overflow: hidden;
        background: transparent;
        transition: all 0.5s ease;
        border-top: 1rpx solid v-bind("config.border.color");
        border-bottom: 1rpx solid v-bind("config.border.color");

        &:last-child {
            margin-bottom: 0;
        }

        .collapse-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 18rpx 24rpx;
            font-weight: 600;
            font-size: 24rpx;
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .header-icon {
            width: 24rpx;
            height: 24rpx;
            display: flex;
            align-items: center;
            justify-content: center;

            .arrow {
                width: 15rpx;
                height: 15rpx;
                border-right: 4rpx solid v-bind("config.border.color");
                border-bottom: 4rpx solid v-bind("config.border.color");
                transform: rotate(45deg);
                transition: transform 0.5s ease;
            }

            .rotated {
                transform: rotate(225deg);
            }
        }

        .collapse-content {
            max-height: 0;
            overflow: hidden;
            transition: max-height 0.5s cubic-bezier(0.4, 0, 0.2, 1), box-shadow 0.5s;
            box-shadow: 0 4rpx 6rpx rgba(0, 0, 0, 0.05);
        }

        .content-inner {
            padding: 24rpx;
            line-height: 1.7;
            transform: translateY(10rpx);
            opacity: 0;
            transition: transform 0.3s ease, opacity 0.3s ease;
        }

        &.expanded .collapse-content {
            max-height: 1000rpx;
        }

        &.expanded .content-inner {
            transform: translateY(0);
            opacity: 1;
            transition: transform 0.5s cubic-bezier(0.4, 0, 0.2, 1), opacity 0.5s cubic-bezier(0.4, 0, 0.2, 1);
        }

        // 响应式设计
        @media (max-width: 768px) {
            .collapse-header {
                padding: 16rpx 20rpx;
                font-size: v-bind("config.fontSize.mediumTitle");
            }

            .content-inner {
                padding: 20rpx;
                font-size: v-bind("config.fontSize.largeText");
            }
        }

        @media (max-width: 480px) {
            .collapse-header {
                padding: 14rpx 16rpx;
                font-size: v-bind("config.fontSize.smallTitle");
            }

            .content-inner {
                padding: 16rpx;
                font-size: v-bind("config.fontSize.mediumText");
            }
        }
    }
}
</style>